package demo1;

public class Solution {

    public int tribonacci(int n) {
        if(n == 0 || n == 1) { return n;}
        if(n == 2) { return 1;}
        int a = 0, b = 1 , c = 1, d = 0;
        for(int i = 3; i <= n; i++){
            d = a + b + c;
            a = b;
            b = c;
            c = d;
        }
        return d;
    }
    public int tribonacci2(int n) {
        if(n == 0 || n == 1) { return n;}
        if(n == 2) { return 1;}

        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        dp[2] = 1;

        for(int i = 3; i <= n; i++){
            dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
        }
        return dp[n];
    }


    public int waysToStep(int n) {
        if(n == 1 || n == 2) { return n;}
        if(n == 3) { return 4;}

        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 4;

        int MOD = (int)1e9 + 7;
        for(int i = 4; i <= n; i++){
            dp[i] = (dp[i - 1] + (dp[i - 2] + dp[i - 3]) % MOD) % MOD;
        }
        return dp[n];
    }


    public int minCostClimbingStairs(int[] cost) {

        int n = cost.length;
        int[] dp = new int[n + 1];

        for(int i = 2; i <= n; i++){
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[n];
    }
    public int minCostClimbingStairs2(int[] cost) {

        int n = cost.length;
        int[] dp = new int[n];
        dp[n - 1] = cost[n - 1];
        dp[n - 2] = cost[n - 2];

        for(int i = n - 3; i >= 0; i--){
            dp[i] = Math.min(dp[i + 1] + cost[i], dp[i + 2] + cost[i]);
        }
        return Math.min(dp[0], dp[1]);
    }


    public int numDecodings(String s) {
        int n = s.length();

        char[] ch = s.toCharArray();
        int[] dp = new int[n];
        if(ch[0] != '0') { dp[0] = 1;}
        if(n == 1) { return dp[0];}
        if(ch[0] != '0' && ch[1] != '0') { dp[1]++;}
        int tmp = (ch[0] - '0') * 10 + (int)(ch[1] - '0');
        if(tmp >= 10 && tmp <= 26) { dp[1]++;}

        for(int i = 2; i < n; i++){
            if(ch[i] != '0') { dp[i] += dp[i - 1];}
            tmp = (ch[i - 1] - '0') * 10 + (int)(ch[i] - '0');
            if(tmp >= 10 && tmp <= 26) { dp[i] += dp[i - 2];}
        }
        return dp[n - 1];
    }
    public int numDecodings2(String s) {

        int n = s.length();
        char[] ch = s.toCharArray();
        int[] dp = new int[n + 1];
        dp[0] = 1;
        if(ch[0] != '0') { dp[1] = 1;}

        for(int i = 2; i <= n; i++){
            if(ch[i - 1] != '0') { dp[i] += dp[i - 1];}
            int tmp = (ch[i - 2] - '0') * 10 + (ch[i - 1] - '0');
            if(tmp >= 10 && tmp <= 26) { dp[i] += dp[i - 2];}
        }
        return dp[n];
    }

}
